package com.gome.axwl.aoc.web.controller.mq.producer;


import com.cloudzone.cloudmq.api.open.base.Msg;
import com.cloudzone.cloudmq.api.open.base.Producer;
import com.cloudzone.cloudmq.api.open.base.SendResult;
import com.cloudzone.cloudmq.api.open.bean.ProducerBean;
import com.gome.commons.entity.MqEntity;
import com.gome.commons.serialization.SerializationService;
import org.apache.commons.lang3.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import javax.annotation.Resource;
import java.util.Properties;


/**
 * 扩展GMQ ProducerBean
 *
 * @author fukui
 */
@Component
public class ProducerBeanImExt extends ProducerBean implements ImProducer {
    public String topic;
    protected Logger logger = LoggerFactory.getLogger(getClass());
    @Autowired
    protected SerializationService serializationService;
    @Autowired
    protected Properties configProperties;



    @Override
    public void start() {
        super.start();
        logger.info("消息队列 Topic : {}  , 初始化结果 : {}", this.getTopic(), isStarted());
    }

    /**
     * @param b 消息序列化字节数组
     */
    @Override
    public SendResult send(String tag, byte[] b) {
        Msg msg = new Msg(topic, tag, b);
        return super.send(msg);
    }

    /**
     * @param b         消息序列化字节数组
     * @param messageId 消息唯一id
     */
    @Override
    public SendResult send(String tag, byte[] b, String messageId) {
        Msg msg = new Msg(topic, tag, b);
        if (!StringUtils.isBlank(messageId)) {
            msg.setKey(messageId);
        }
        return super.send(msg);
    }

    @Override
    public SendResult send(String tag, MqEntity<?> mqEntity) {
        return send(tag, serializationService.serialize(mqEntity));
    }


    public String getTopic() {
        return topic;
    }

    public void setTopic(String topic) {
        this.topic = topic;
    }

    public void setSerializationService(SerializationService serializationService) {
        this.serializationService = serializationService;
    }
}
